﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using KaKaSongGame.ViewModel;
using KKSGameLibrary.Logic;

namespace KaKaSongGame
{
    public partial class PlayerList : UserControl,IPlayerActionUI
    {
        public PlayerList()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(PlayerList_Loaded);
        }

        void PlayerList_Loaded(object sender, RoutedEventArgs e)
        {
            MainPageVM = this.DataContext as MainPageViewModel;
            //foreach (var gpvm in MainPageVM.PlayerList)
            //{
            //    gpvm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(gpvm_PropertyChanged);
            //}
        }

        //void gpvm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        //{
        //    var playervm = sender as PlayerViewModel;
        //    var container = GetPlayerContainer(playervm.GamePlayer.ID);
        //    var headercontainer = GetPlayerHeaderContainer(playervm.GamePlayer.ID);

        //    if(e.PropertyName=="PlayerState")
        //    {
        //        var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;
        //        if (playervm.PlayerState == PlayerVMState.PutCard ||
        //            playervm.PlayerState == PlayerVMState.PutPuppet)
        //        {
        //            actiontimer.ActiveTimer(playervm.PlayerActionTimeout, delegate
        //            {
        //                var cardview = container.Children.FirstOrDefault(ele => ele is GameCard);
        //                if (cardview != null)
        //                    container.Children.Remove(cardview);
        //            });
        //            accordion1.SelectedItem = playervm;
        //        }
        //        else if (playervm.PlayerState == PlayerVMState.PutCardComplete ||
        //            playervm.PlayerState==PlayerVMState.PutPuppetComplete)
        //        {
        //            actiontimer.DeactiveTimer();
        //        }
        //    }
        //    else if (e.PropertyName == "CardInMap")
        //    {
                
        //        if (playervm.CardInMap != null)
        //        {
        //            var cardview = new GameCard();
        //            cardview.CardData = playervm.CardInMap;
        //            container.Children.Add(cardview);
        //        }
        //    }
        //}

        public MainPageViewModel MainPageVM { set; get; }

        public Panel GetPlayerContainer(string playerid)
        {
            return _playercontainerdict[playerid];
        }

        Panel GetPlayerHeaderContainer(string playerid)
        {
            return _playerheadercontainterdict[playerid];
        }

        Dictionary<string, Panel> _playercontainerdict = new Dictionary<string, Panel>();
        private void GPlayerContainer_Loaded(object sender, RoutedEventArgs e)
        {
            var container = sender as Panel;
            var gpvm = container.DataContext as PlayerViewModel;
            _playercontainerdict[gpvm.GamePlayer.ID] = container;

            var pincontainer = container.FindName("pinsp") as Panel;
            for (int i = 0; i < MainPageVM.PuppetNumber; i++)
            {
                var pin = new PlayerPin
                {
                    PuppetColor = gpvm.PlayerColor,
                    Width = 30,
                    Height = 30
                };

                var fb = new Microsoft.Expression.Interactivity.Layout.FluidMoveBehavior();
                fb.Duration = TimeSpan.FromMilliseconds(1000);
                fb.EaseX = new CircleEase { EasingMode = EasingMode.EaseOut };
                fb.EaseY = new CircleEase { EasingMode = EasingMode.EaseOut };
                fb.Attach(pin);

                pincontainer.Children.Add(pin);
            }
        }

        Dictionary<string, Panel> _playerheadercontainterdict = new Dictionary<string, Panel>();
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            var headercontainer = sender as Panel;
            var gpvm = headercontainer.DataContext as PlayerViewModel;
            _playerheadercontainterdict[gpvm.GamePlayer.ID] = headercontainer;
        }

        PlayerViewModel getPlayerVM(string id)
        {
            return MainPageVM.PlayerList.First(p => p.GamePlayer.ID == id);
        }

        public void SetOwner(int row, int col, OpenDirection dir, GamePlayer player)
        {
            var headercontainer = GetPlayerHeaderContainer(player.ID);
            var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;

            actiontimer.DeactiveTimer(false);
        }

        public void SetCard(CardInMap cardim, GamePlayer player)
        {
            var headercontainer = GetPlayerHeaderContainer(player.ID);
            var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;

            actiontimer.DeactiveTimer(false);
        }

        public void SetCardCandidate(Card card, GamePlayer player)
        {
            var container = GetPlayerContainer(player.ID);
            var headercontainer = GetPlayerHeaderContainer(player.ID);
            var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;
            var playervm = getPlayerVM(player.ID);

            // put card
            var cardview = new GameCard();
            cardview.Width = 60;
            cardview.Height = 60;
            cardview.CardData = new CardInMap
            {
                Card = card,
            };

            var fb = new Microsoft.Expression.Interactivity.Layout.FluidMoveBehavior();
            fb.Duration = TimeSpan.FromMilliseconds(1000);
            fb.EaseX = new CircleEase { EasingMode = EasingMode.EaseOut };
            fb.EaseY = new CircleEase { EasingMode = EasingMode.EaseOut };
            fb.Attach(cardview);

            container.Children.Add(cardview);

            // start timer
            actiontimer.ActiveTimer(MainPageVM.ActionTimeout, delegate
            {
                if (container.Children.Contains(cardview))
                {
                    fb.Detach();
                    container.Children.Remove(cardview);
                }
            });
            accordion1.SelectedItem = playervm;
        }

        public void SetPuppetCandidate(GamePlayer player)
        {
            var headercontainer = GetPlayerHeaderContainer(player.ID);
            var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;
            var playervm = getPlayerVM(player.ID);

            // start timer
            actiontimer.ActiveTimer(MainPageVM.ActionTimeout, delegate
            {
                ;
            });

            accordion1.SelectedItem = playervm;
        }

        public void PlayerActionTimeout(GamePlayer player)
        {
            var headercontainer = GetPlayerHeaderContainer(player.ID);
            var actiontimer = headercontainer.FindName("actiontimer") as ActionTimer;

            actiontimer.DeactiveTimer(true);
        }
    }
}
